package com.ponhu.ea2.goodsCenter.provider.facade.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ponhu.ea2.common.basic.model.PagedResult;
import com.ponhu.ea2.goodsCenter.provider.mapper.JuWoDataMapper;
import com.ponhu.ea2.goodsCenter.provider.utils.LocalDateUtil;
import com.ponhu.ea2.query.ApplyingQuery;
import com.ponhu.ea2.service.facade.ApplyingStockDataService;
import com.ponhu.ea2.vo.ApplyingStockDataVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;

@Slf4j
@DubboService(version = "1.0.0", interfaceClass = ApplyingStockDataService.class, timeout = 6000)
public class ApplyingStockDataServiceImpl implements ApplyingStockDataService {

    @Resource
    JuWoDataMapper juWoDataMapper;

    /**
     * 查询巨沃调拨数据
     *
     * @param applyingQuery {@link com.ponhu.ea2.query.ApplyingQuery}
     * @return
     */
    @Override
    public PagedResult<ApplyingStockDataVo> getApplyingStockData(ApplyingQuery applyingQuery) {
        Page<ApplyingStockDataVo> applyingQueryPage = new Page<>();
        applyingQueryPage.setCurrent(applyingQuery.getPageIndex());
        applyingQueryPage.setSize(applyingQuery.getPageSize());

        // 调拨时间区间判断
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.between(StringUtils.isNotBlank(applyingQuery.getApplyStartTime()) && StringUtils.isNotBlank(applyingQuery.getApplyEndTime()), "crdt",
                StringUtils.isNotBlank(applyingQuery.getApplyStartTime()) ? LocalDateUtil.localDateTimeParse(applyingQuery.getApplyStartTime(), LocalDateUtil.FORMAT_PATTERN1) : "",
                StringUtils.isNotBlank(applyingQuery.getApplyEndTime()) ? LocalDateUtil.localDateTimeParse(applyingQuery.getApplyEndTime(), LocalDateUtil.FORMAT_PATTERN1) : "");
        if(StringUtils.isNotBlank(applyingQuery.getApplyNo())){
            wrapper.eq("biid",applyingQuery.getApplyNo());
        }
        IPage<ApplyingStockDataVo> iPage = juWoDataMapper.selectPage(applyingQueryPage, wrapper);
        // 分页
        return new PagedResult<>(iPage.getRecords(), (int) iPage.getCurrent(), iPage.getTotal(), (int) iPage.getSize());
    }
}
